package cn.zxy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {

    static SqlSessionFactory sqlSessionFactory;
    static InputStream is;
    static {
        try {
           is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder factorBuilder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = factorBuilder.build(is);

        } catch (IOException e) {
           e.printStackTrace();
        }
    }

    //获取session域对象 获取自动提交
    public static SqlSession getSqlSession(boolean autocommit){
        return sqlSessionFactory.openSession(autocommit);
    }

    //关闭
    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.close();

        try {
            is.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }









}
